package cn.iocoder.yudao.module.english.controller.admin.questionbank;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.english.controller.admin.questionbank.vo.QuestionBankPageReqVO;
import cn.iocoder.yudao.module.english.controller.admin.questionbank.vo.QuestionBankRespVO;
import cn.iocoder.yudao.module.english.controller.admin.questionbank.vo.QuestionBankSaveReqVO;
import cn.iocoder.yudao.module.english.dal.dataobject.questionbank.QuestionBankDO;
import cn.iocoder.yudao.module.english.service.questionbank.QuestionBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 题库")
@RestController
@RequestMapping("/english/question-bank")
@Validated
public class QuestionBankController {

    @Resource
    private QuestionBankService questionBankService;

    @PostMapping("/create")
    @Operation(summary = "创建题库")
    @PreAuthorize("@ss.hasPermission('english:question-bank:create')")
    public CommonResult<Long> createQuestionBank(@Valid @RequestBody QuestionBankSaveReqVO createReqVO) {
        return success(questionBankService.createQuestionBank(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新题库")
    @PreAuthorize("@ss.hasPermission('english:question-bank:update')")
    public CommonResult<Boolean> updateQuestionBank(@Valid @RequestBody QuestionBankSaveReqVO updateReqVO) {
        questionBankService.updateQuestionBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除题库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('english:question-bank:delete')")
    public CommonResult<Boolean> deleteQuestionBank(@RequestParam("id") Long id) {
        questionBankService.deleteQuestionBank(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得题库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('english:question-bank:query')")
    public CommonResult<QuestionBankRespVO> getQuestionBank(@RequestParam("id") Long id) {
        QuestionBankDO questionBank = questionBankService.getQuestionBank(id);
        return success(BeanUtils.toBean(questionBank, QuestionBankRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得题库分页")
    @PreAuthorize("@ss.hasPermission('english:question-bank:query')")
    public CommonResult<PageResult<QuestionBankRespVO>> getQuestionBankPage(@Valid QuestionBankPageReqVO pageReqVO) {
        PageResult<QuestionBankDO> pageResult = questionBankService.getQuestionBankPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionBankRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出题库 Excel")
    @PreAuthorize("@ss.hasPermission('english:question-bank:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuestionBankExcel(@Valid QuestionBankPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuestionBankDO> list = questionBankService.getQuestionBankPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "题库.xls", "数据", QuestionBankRespVO.class,
                BeanUtils.toBean(list, QuestionBankRespVO.class));
    }

}